package com.cat.dataStructure03;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-operations-to-make-median-of-array-equal-to-k/
 * @create 2025/11/10 15:37
 * @since JDK17
 */

public class Solution36 {
    public long minOperationsToMakeMedianK(int[] nums, int k) {
        long ans = 0;
        Arrays.sort(nums);
        int n = nums.length, mid = nums[n / 2];
        if (mid < k) {  //
            for (int i = n / 2; i < n && nums[i] < k; i++) {
                ans += k - nums[i];
            }
        } else {    //
            for (int i = n / 2; i >= 0 && nums[i] > k; i--) {
                ans += nums[i] - k;
            }
        }

        return ans;
    }
}
